package cn.shop.alibaba.service.impl;

import cn.shop.alibaba.beans.Order;
import cn.shop.alibaba.beans.OrderItem;
import cn.shop.alibaba.beans.Product;
import cn.shop.alibaba.beans.User;
import cn.shop.alibaba.constants.HttpCode;
import cn.shop.alibaba.dto.OderParams;
import cn.shop.alibaba.mapper.OrderItemMapper;
import cn.shop.alibaba.mapper.OrderMapper;
import cn.shop.alibaba.resp.Result;
import cn.shop.alibaba.service.OrderService;
import com.alibaba.fastjson.JSONObject;
import com.mysql.cj.util.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.client.RestTemplate;

import java.math.BigDecimal;

@Slf4j
@Service
public class OrderServiceImpl implements OrderService {

    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private OrderItemMapper orderItemMapper;
    @Autowired
    private RestTemplate restTemplate;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveOrder(OderParams oderParams) {
        if (ObjectUtils.isEmpty(oderParams)){
            throw new RuntimeException("参数异常: " + JSONObject.toJSONString(oderParams));
        }
        User user =  restTemplate.getForObject("http://localhost:8060/user/user/"+oderParams.getId(), User.class);
        if (user == null) {
            throw new RuntimeException("未获取到用户信息: " + JSONObject.toJSONString(oderParams));
        }
        Product product = restTemplate.getForObject("http://localhost:8070/product/product/"+oderParams.getProId(), Product.class);
        if (product == null){
            throw new RuntimeException("未获取到商品信息: " + JSONObject.toJSONString(oderParams));
        }
        if (product.getProStock() < oderParams.getNumber()){
            throw new RuntimeException("商品库存不足: " + JSONObject.toJSONString(oderParams));
        }
        Order order = new Order();
        order.setAddress(user.getAddress());
        order.setPhone(user.getPhone());
        order.setUserId(user.getId());
        order.setUsername(user.getUsername());
        order.setTotalPrice(product.getProPrice().multiply(BigDecimal.valueOf(oderParams.getNumber())));
        orderMapper.insert(order);

        OrderItem orderItem = new OrderItem();
        orderItem.setNumber(oderParams.getNumber());
        orderItem.setOrderId(order.getId());
        orderItem.setProId(product.getId());
        orderItem.setProName(product.getProName());
        orderItem.setProPrice(product.getProPrice());
        orderItemMapper.insert(orderItem);

        Result<Integer> result = restTemplate.getForObject("http://localhost:8070/product/update_count/" + oderParams.getProId() + "/" + oderParams.getNumber(), Result.class);
        if (result.getCode() != HttpCode.SUCCESS){
            throw new RuntimeException("库存扣减失败");
        }
        log.info("库存扣减成功");

    }
}
